// 二叉树
function BST() {
  this.root = null
  this.insert = insert
  this.inOrder = inOrder
}

function Node(data, left, right) {
  this.data = data
  this.left = left
  this.right = right
  this.show = show
}

function show() {
  return this.data
}

function insert(data) {
  let n new Node(data, null, null)

  if (this.root === null) {
    this.root = n
  } else {
    let current = this.root
    let parent
    while (true) {
      parent = current
      if (data < current.data) {
        current = current.left
        if (current === null) {
          parent.left = n
          break
        }
      } else {
        current = current.right
        if (current === null) {
          parent.right = n
          break
        }
      }
    }
  }
}

// 中序遍历
function inOrder(node) {
  if (node !== null) {
    inOrder(node.left)
    console.log(node.show())
    inOrder(node.right)
  }
}

// 先序遍历
function inOrder(node) {
  if(!(node == null)) {
    console.log(node.show());
    inOrder(node.left);
    inOrder(node.right);
  }
}

// 后序遍历
function inOrder(node) {
  if(!(node == null)) {
    inOrder(node.left);
    inOrder(node.right);
    console.log(node.show());
  }
}
